<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\ReferBlackUrl;
use App\UserApp;
use Illuminate\Http\Request;

/**
 * refer 黑名单
 * Class ReferController
 *
 * @package App\Http\Controllers\Admin
 */
class ReferController extends Controller
{
    /**
     * @Desc: refer列表
     * @Author: hank <maokeyang1031@gmail.com>
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function referList(Request $request)
    {
        $wd = trim($request->input('wd'));

        $model = new ReferBlackUrl();
        if (!empty($wd)) {
            $model = $model->where("url", "like", "%$wd%");
        }

        if (!session('admin')->hasSuperRole()) {
            $uid = session("admin")->getUid();
            $model = $model->where("user_id", $uid);
        }


        $list = $model->with("adminUser")->where("del_flag", 0)->paginate(10);
        return view('admin.refer.index', ['list' => $list, 'wd' => $wd]);
    }


    /**
     * @Desc: 添加refer
     * @Author: hank <maokeyang1031@gmail.com>
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function referAddView(Request $request)
    {
        $uid = session("admin")->getUid();

        $userApp = UserApp::where(["user_id" => $uid, "del_flag" => 0])->first();

        return view('admin.refer.add', compact('userApp'));
    }

    /**
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return mixed
     */
    public function referAdd(Request $request)
    {
        $data   = $request->post();
        $config = new ReferBlackUrl();

        $data["user_id"] = session("admin")->getUid();
        $data["operate_type"] = session("admin")->hasSuperRole() ? 2 : 1;

        $config->fill($data)->save();
        return $this->json(200, '添加成功');
    }

    /**
     * @Desc: 修改应用信息
     * @Author: hank <maokeyang1031@gmail.com>
     * @param Request $request
     * @param $id
     * @return \Illuminate\View\View
     */
    public function referUpdateView(Request $request, $id)
    {
        return view('admin.refer.update', ['config' => ReferBlackUrl::findOrFail($id)]);
    }

    public function referUpdate(Request $request, $id)
    {
        $config = ReferBlackUrl::findOrFail($id);

        if (!session('admin')->hasSuperRole()) {
            $uid = session("admin")->getUid();
            if ($uid != $config->user_id) {
                return abort(403, "非法请求");
            }
        }

        $data   = $request->post();
        $config->fill($data);
        $config->save();
        return $this->json(200, '修改成功');
    }

    /**
     * @Desc: 删除refer
     * @Author: hank <maokeyang1031@gmail.com>
     * @param $id
     * @return mixed
     */
    public function referDel($id)
    {
        $referResult = ReferBlackUrl::where("id", $id)->first();
        if (empty($referResult)) {
            return abort("404", "参数错误");
        }
        if (!session('admin')->hasSuperRole()) {
            $uid = session("admin")->getUid();
            if ($uid != $referResult->user_id) {
                return abort(403, "非法请求");
            }
        }

        ReferBlackUrl::where("id", $id)->update(["del_flag" => 1]);
        return $this->json(200, '删除成功');
    }
}
